﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SQLt
{


    public partial class ConODBC : Form, iConnectionStringDialog
    {
        private bool isFormLoaded = false;
        private string UID { get; set; }
        private string PID { get; set; }
        private string DSN { get; set; }

        public ConODBC()
        {
            InitializeComponent();
        }

        public void SaveOptions()
        {
            Microsoft.Win32.RegistryKey SearchKey = Application.UserAppDataRegistry.OpenSubKey("Options\\ODBC", true);
            if (SearchKey != null)
            {
                SearchKey.SetValue("DSN", DSN);
                SearchKey.SetValue("UID", UID);
                SearchKey.SetValue("PID", PID);
            }

        }

        public void LoadOptions()
        {
            Application.UserAppDataRegistry.CreateSubKey("Options\\ODBC");
            Microsoft.Win32.RegistryKey SearchKey = Application.UserAppDataRegistry.OpenSubKey("Options\\ODBC");
            if (SearchKey != null)
            {
                DSN = SearchKey.GetValue("DSN", "").ToString();
                UID = SearchKey.GetValue("UID", "informix").ToString();
                PID = SearchKey.GetValue("PID", "informix").ToString();
            }


        }

        private void OnLoad(object sender, EventArgs e)
        {
            //get system DSN
            Microsoft.Win32.RegistryKey SearchKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources");
            if (SearchKey != null)
            {
                foreach (string DsnName in SearchKey.GetValueNames())
                {
                    cDSNlist.Items.Add(DsnName);
                }
            }
            //get User DSN
            SearchKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources");
            if (SearchKey != null)
            {
                foreach (string DsnName in SearchKey.GetValueNames())
                {
                    cDSNlist.Items.Add(DsnName);
                }
            }

            LoadOptions();
            isFormLoaded = true;
        }

        public string getConnectionString()
        {
            string result = "Dsn=" + DSN+";Uid="+UID+";Pwd="+PID+";";
            return result;
        }

        private void OnOK(object sender, EventArgs e)
        {
            DSN = cDSNlist.SelectedItem.ToString();
            UID = txtUser.Text;
            PID = txtPassword.Text;

            SaveOptions();

            if (DSN.Length>0)
                this.Close();
        }

        private void OnDSNChanged(object sender, EventArgs e)
        {
            if(isFormLoaded )
            DSN = cDSNlist.SelectedItem.ToString();
        }

        private void OnActivated(object sender, EventArgs e)
        {
            int n = cDSNlist.Items.Count;
            for (int i = 0; i < n; i++)
            {
                if (cDSNlist.Items[i].ToString() == DSN)
                {
                    cDSNlist.SelectedIndex = i;
                }
            }
            if (UID.Length > 0) txtUser.Text = UID;
            if (PID.Length > 0) txtPassword.Text = PID;
        }
    }
}
